package web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2022-04-21-16:14 16:14
 */
@WebServlet("/findUserServlet")
public class FindUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        HashMap<String, Object> hashMap = new HashMap<>();
        if ("chai".equals(username)){
            //已经拥有该用户名
            //期望返回的data值
            //      {"userExsit":true,"msg":"此用户名重复了"}
            //      {"userExsit":false,"msg":"用户名可用"}
            hashMap.put("userExsit",true);
            hashMap.put("msg","此用户名重复了");
        }else{
            hashMap.put("userExsit",false);
            hashMap.put("msg","用户名可用");
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getWriter(),hashMap);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
